
/*
 * GET home page.
 */
var pageSize = 5;


exports.index = function(req, res){
	if(req.session['access']==undefined){
		var ip = req.headers['x-real-ip'];
		var agent = req.headers['user-agent'];
		var time = parseInt(new Date() /1000);
		req.session['access'] = time;
		var log_sql = 'insert into ??? set hello=wolrd';
		conn.query(log_sql,function(err,result){
		if(err){
			console.log(err);
		}else{
			//	
		}
		});
	}
	
	var sql ='语句不给看';

	conn.query(sql,function(err,result){
		if(err){
			console.log(err);
			
		}else{
			console.log(result);
			var is_end = 0;
			if(result.length<pageSize)is_end = 1;
			res.render('index', { data: result,end:is_end });
		}
	});
    
};

//��ť����
exports.op = function(req,res){
	var op = req.body.op.op;
	var id = req.body.op.id;
	var key = 'last'+id;
	if(req.session[key]==undefined){
		req.session[key] = parseInt(new Date() /1000);
		
		doOp(op,id,res);
		
	}else{
		var now = parseInt(new Date() /1000);
		if(now-req.session[key]>60){
			req.session[key] = now;
			
			doOp(op,id,res);
			
		}else{
			console.log(req.session);
			res.end();
		}
	}
	
	//console.log(req.session.last);
};

exports.loadmore = function(req,res){
	var id = req.body.id;
	var sql ='语句不给看';
	//console.log(sql);
	conn.query({"sql":sql,"params":{"id":id}},function(err,result){
		if(err){
			console.log(err);
			
		}else{
			if(result.length==0){
				res.end('-1');
			}else{
			var is_end = 0;
			if(result.length<pageSize)is_end = 1;
			
			var data = {'data':result,'last':result[result.length-1].id,'end':is_end};
			//console.log(data);
			res.end(JSON.stringify(data));
			}
		}
	});
};

exports.staticpage = function(req,res){
	var page = req.body.page;
	res.render(page);
};

//����
exports.search = function(req,res){
	var kw = '%'+req.query.kw+'%';
	var sql ='语句不给看';
	conn.query({"sql":sql,"params":{"word":kw}},function(err,result){
		if(err){
			console.log(err);
			
		}else{
			console.log(result);
			res.render('index', { data: result,end:1 });
		}
	});
};

//ִ�а�ť����
var doOp = function(op,id,res){
	if(op=='up'){
		var sql ='语句不给看';
	}else if(op=='down'){
		var sql ='语句不给看';
	}else{
		var sql = 'select 1';//hb
		console.log('do op err:op:'+op+' not defined');
	}
	console.log(sql);
	conn.query({"sql":sql,"params":{"fid":id}},function(err,result){
		
		res.end('1');
	});
	
};
